package com.dchh.android.suverynew.presenter.impl;

import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.module.local.ITaskMainMoudle;
import com.dchh.android.suverynew.module.local.impl.TaskMainMoudle;
import com.dchh.android.suverynew.presenter.ITaskMainPresenter;
import com.dchh.android.suverynew.view.ITaskMainView;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.Task;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;

import java.util.List;

/**
 * Created by eric on 2016/8/10.
 */
public class TaskMainPresenter implements ITaskMainPresenter {
    private ITaskMainView mTaskMainView;
    private ITaskMainMoudle mTaskMainMoudle;

    public TaskMainPresenter(ITaskMainView mTaskMainView) {
        this.mTaskMainView = mTaskMainView;
        this.mTaskMainMoudle = new TaskMainMoudle();
    }

    @Override
    public void initData() {
        String root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
        List<Task> downList = CommenUtil.listTask(root);
        // 判断列表是否为空
        this.mTaskMainView.isNoData(downList.size() < 0);
        this.mTaskMainView.onInitDataSuccess(downList);
        for (Task task : downList) {
            String path = root + "/" + task.getTitle();
            mTaskMainMoudle.upDataDICZW(path);
        }
    }

    @Override
    public void restore(List<Task> downList) {
        boolean ef = SPreferencesManager.getValue(MSApplication.EXCEPTION_FLAG, false);
        if (ef) {//有异常-- 恢复
            String tn = SPreferencesManager.getValue(MSApplication.TASK, "");
            for (int i = 0; i < downList.size(); i++) {
                Task task = downList.get(i);
                if (task.getTitle().equals(tn)) {
                    Constants.currentTask = task;
                    break;
                }
            }
        } else if (downList.size() == 1) {//只有一个任务包--直接跳转到调查期选择
            Constants.currentTask = downList.get(0);
        } else {//多个任务包---不用再往下执行了
            return;
        }
        // 载入进度框
        Constants.currentCounty = Constants.currentTask.getCounty();
        this.mTaskMainView.onRestoreSuccess(Constants.currentTask.getTitle());
    }
}
